/**
 * Created by fengliu on 15/8/29.
 */
'use strict';
import React from 'react';
import AMUIRect from 'amazeui-react';
var Container = AMUIRect.Container;
var Slider = AMUIRect.Slider;
var Panel = AMUIRect.Panel;
var List = AMUIRect.List;
var ListItem = AMUIRect.ListItem;
var Badge = AMUIRect.Badge;
var Thumbnail = AMUIRect.Thumbnail;
var Pagination = AMUIRect.Pagination;
var Grid=AMUIRect.Grid;
var Col=AMUIRect.Col;
module.exports = React.createClass({
    render(){
        return ;
    }
});



var ListArticle = React.createClass({
    loadPPfromCoding(){
      // alert(URL.getHotPP);
      // 获取最新冒泡 (GET https://coding.net/api/tweet/public_tweets)
      jQuery.ajax({
        // url: "https://coding.net/api/tweet/public_tweets",
         url: "https://oevncl-80-fdzjii.box.myide.io/rewrit.php",
          //url: "/rewrit.php",
          type: "POST",
          data: {
              "filter": "true",
              "size": "20",
              "sort": "time",
              type:'getNewPP'
          },
          dataType:'json'
      })
      .done(function(data, textStatus, jqXHR) {
          //console.log("HTTP Request Succeeded: " + jqXHR.status);
          // console.log(data);
          //console.log('code',data.code);
          if(data.code==0){
            //console.log('fetch Succeeded');
            this.setState({data: data.data});
            // console.log("shis.data",this.state.data);
          }else {
            //console.log("fetch data error");
          }
      }.bind(this))
      .fail(function(jqXHR, textStatus, errorThrown) {
          console.log("HTTP Request Failed");
          console.log(jqXHR);
          console.log(errorThrown.toString());
      }.bind(this))
      .always(function() {

      });
    },
    getInitialState(){
      return {data:[]};
    },
    componentDidMount: function() {
      this.loadPPfromCoding();
      // console.log(this.data);

    },
    render(){
        var data=this.state.data;
        var htmNodes=[];
        $.each(data,function (key,value) {
          var ownerUrl=URL.coding + value.owner.path;
          var avatar=value.owner.avatar.replace(/\/static\/fruit_avatar\//g,URL.coding+'/static/fruit_avatar/');
          // console.log(avatar);
          //替换value.content中不完整的url
          // href="/pp/
          var content=value.content.replace(/href=\"\/pp\//g,'target=\'_blank\' href="'+URL.coding+"/pp/");
          content=content.replace(/class="bubble-markdown-image-link"/g,"class=\"bubble-markdown-image-link\"  onClick=\"imageClick(this)\"");
          var ppUrl=URL.coding+value.owner.path+"/pp/"+value.id;
          var style={display:"block","text-align":"center"};
          var caption=<a href={ppUrl} style={style} target="_blank">
                {value.owner.name}
                <br /><Badge amStyle="primary" round> {value.comments}</Badge>  <Badge amStyle="success" round> {value.likes}</Badge>
                </a>;
          htmNodes[key]= <ListItem>
            <Grid>
              <Col sm={3}>
                <Thumbnail sm={3}
                  href={ownerUrl}
                  caption={caption}
                  src= {avatar} />
                  </Col>
                  <Col sm={9}  dangerouslySetInnerHTML={{__html: content}}>
                  </Col>
            </Grid>
              </ListItem>;
        });
        // console.log("nodes",htmNodes);
        return <Panel header="最新冒泡" amStyle="primary">
            <List border static>
              {htmNodes}
            </List>
        </Panel>;
    }
});
